<template>
  <div id="app">
    <a-input
      placeholder="请输入任务"
      class="my_ipt"
      :value="inpVal"
      @change="handleInpChange"
    />
    <a-button type="primary" @click="addItemToList(inpVal)">添加事项</a-button>

    <a-list bordered :dataSource="infoList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked="item.done" @change="(e) => {cbStatusChange(e,item.id)}">{{ item.info }}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="delItemById(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{unDoneLength}}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button :type="viewItem === 'all' ? 'primary' : 'default'" @click="handleItemChange('all')">全部</a-button>
          <a-button :type="viewItem === 'undone' ? 'primary' : 'default'" @click="handleItemChange('undone')">未完成</a-button>
          <a-button :type="viewItem === 'done' ? 'primary' : 'default'" @click="handleItemChange('done')">已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="clean">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapGetters } from "vuex";
export default {
  name: "app",
  data() {
    return {};
  },
  created() {
    this.$store.dispatch("getList");
  },
  computed: {
    ...mapState([ "inpVal","viewItem"]),
    ...mapGetters(["unDoneLength", "infoList"])
  },
  methods: {
    // 监听文本框变化 触发
    handleInpChange(e) {
      // 防抖
      setTimeout(() => {
        console.log(e.target.value);
        this.$store.commit("initInpVal", e.target.value);
      }, 500);
    },
    // 向列表新增 item 项
    addItemToList(e) {
      if (e.trim().length == 0) {
        return this.$message.warning("输入框不能为空！");
      }
      this.$store.commit("addItem", e);
    },
    // 根据 ID 删除 item
    delItemById(id){
      console.log(id)
      this.$store.commit("delItem", id)
    },
    // 监听 复选框变化 触发
    cbStatusChange(e,id){
      // 通过 e.target.checked 可以接收到复选框最新的 状态
      console.log(e.target.checked)
      console.log(id)
      let params = {
        id: id,
        status: e.target.checked
      }
      this.$store.commit("changeStatus", params)
    },
    // 清除已完成
    clean(){
      this.$store.commit("delDone")
    },
    // 监听菜单变化
    handleItemChange(e){
      console.log(e)
      this.$store.commit("changeNav", e)
    }
  },
};
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
